// BatchProcessor.h
#pragma once
#include <Windows.h>
#include <vector>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <chrono>
#include <thread>
#include <functional>

struct BatchOperation {
    enum Type { READ, WRITE } type;
    std::wstring path;
    size_t offset;
    size_t length;
    std::vector<uint8_t> buffer;
    std::function<void(NTSTATUS)> callback;
    
    // 用于排序和合并
    bool operator<(const BatchOperation& other) const {
        if (path != other.path) return path < other.path;
        if (type != other.type) return type < other.type;
        return offset < other.offset;
    }
};

class BatchProcessor {
public:
    BatchProcessor(size_t maxBatchSize = 64 * 1024, // 64KB默认批处理大小
                  std::chrono::milliseconds maxDelay = std::chrono::milliseconds(5));
    ~BatchProcessor();

    // 添加操作到批处理队列
    void AddOperation(BatchOperation op);
    
    // 手动触发批处理
    void ProcessBatch();

private:
    void ProcessorThread();
    void ProcessBatchInternal(std::vector<BatchOperation>& batch);
    bool TryMergeOperations(std::vector<BatchOperation>& batch);

    const size_t m_maxBatchSize;
    const std::chrono::milliseconds m_maxDelay;
    
    std::queue<BatchOperation> m_pendingOps;
    std::mutex m_mutex;
    std::condition_variable m_condition;
    
    bool m_shutdown;
    std::thread m_processorThread;
    
    // 性能统计
    size_t m_totalOps;
    size_t m_batchedOps;
};